#include "vmainwindow.h"
#include "voptionsbar.h"
#include "vchatinterface.h"
#include "vcontactsinterface.h"

#include <QHBoxLayout>
#include <QPaintEvent>
#include <QPainter>
#include <QPainterPath>

VMainWindow::VMainWindow(QWidget* parent)
    : QWidget(parent)
    , m_pOptionBar(new VOptionsBar(this))
    , m_pHBoxLayout(new QHBoxLayout(this))
    , m_pChatInterface(new VChatInterface(this))
    , m_pContactsInterface(new VContactsInterface(this))
{
    setMinimumSize(676, 500);
    resize(900, 640);
	setWindowFlags(Qt::FramelessWindowHint | Qt::Window); // 设置为无边框窗口
    setAttribute(Qt::WA_TranslucentBackground);
    initLayout();
    initConnect();
}

VMainWindow::~VMainWindow()
{}


void VMainWindow::initConnect()
{
    connect(m_pOptionBar, &VOptionsBar::optionSeleted, this, &VMainWindow::onOptionSeleted);
}

void VMainWindow::initLayout()
{
    setMainWindow(m_pChatInterface);
    m_pHBoxLayout->setContentsMargins(1, 1, 1, 1);
    m_pHBoxLayout->setSpacing(0);
    m_pHBoxLayout->addWidget(m_pOptionBar);
    m_pHBoxLayout->addWidget(m_pMainWindow);
}

void VMainWindow::onOptionSeleted(OPTIONS option)
{
    switch (option)
    {
    case OPTIONS::CHAT:setMainWindow(m_pChatInterface);
        break;
    case OPTIONS::CONTACT:setMainWindow(m_pContactsInterface);
        break;
    case OPTIONS::COLLECTION:
        break;
    case OPTIONS::MOMENTS:
        break;
    }
}

bool VMainWindow::nativeEvent(const QByteArray& eventType, void* message, qintptr* result)
{
    static int s_border = 5;
    Q_UNUSED(eventType)

        MSG* param = static_cast<MSG*>(message);
    switch (param->message)
    {
    case WM_NCHITTEST:
    {
        int nX = (int)(short)LOWORD(param->lParam) - geometry().x();
        int nY = (int)(short)HIWORD(param->lParam) - geometry().y();

        *result = HTCAPTION;

        if ((nX > 0) && (nX < s_border) && (nY > 0) && (nY < s_border))
            *result = HTTOPLEFT;
        else if ((nX > width() - s_border) && (nX < width()) && (nY > 0) && (nY < s_border))
            *result = HTTOPRIGHT;
        else if ((nX > 0) && (nX < s_border) && (nY > height() - s_border) && (nY < height()))
            *result = HTBOTTOMLEFT;
        else if ((nX > width() - s_border) && (nX < width()) && (nY > height() - s_border) && (nY < height()))
            *result = HTBOTTOMRIGHT;
        else if ((nX > 0) && (nX < s_border))
            *result = HTLEFT;
        else if ((nX > width() - s_border) && (nX < width()))
            *result = HTRIGHT;
        else if ((nY > 0) && (nY < s_border))
            *result = HTTOP;
        else if ((nY > height() - s_border) && (nY < height()))
            *result = HTBOTTOM;
        else if (nY > 50)
            return QWidget::nativeEvent(eventType, message, result);
        return m_pOptionBar->childAt(nX, nY) == nullptr
            && m_pMainWindow->childAt(QPoint(nX, nY) - m_pMainWindow->pos()) == nullptr;
    }
    }
    return QWidget::nativeEvent(eventType, message, result);
}

void VMainWindow::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    if (isMaximized())
    {
        m_pHBoxLayout->setContentsMargins(0, 0, 0, 0);
        painter.setPen(Qt::NoPen);
    }
    else
    {
        m_pHBoxLayout->setContentsMargins(1, 1, 1, 1);
        painter.setPen(QPen(QColor(0, 0, 0, 38), 1));
    }
    painter.drawRect(0,0,width()-1,height()-1);
}

void VMainWindow::setMainWindow(QWidget* window)
{
    m_pHBoxLayout->removeWidget(m_pMainWindow);
    m_pChatInterface->hide();
    m_pContactsInterface->hide();
    
    m_pMainWindow = window;
    m_pMainWindow->show();
    m_pHBoxLayout->addWidget(m_pMainWindow);
    
}

VMainWindow* MainWindow()
{
    static VMainWindow instance;
    return &instance;
}